package cn.edu.scau.cmi.ningkehua.client;

import cn.edu.scau.cmi.ningkehua.singleton.Chairman;
import cn.edu.scau.cmi.ningkehua.singleton.Marshal;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class SingletonClient extends Application implements EventHandler<MouseEvent> {
	Button button1 = new Button("创建");
	Button button2 = new Button("创建");
	TextField text1 = new TextField();
	TextField text2 = new TextField();
	TextArea text11 = new TextArea();
	TextArea text22 = new TextArea();
	Chairman chairman;
	Marshal marshal;
	int count1=0;
	int count2=0;
	
	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage primaryStage) throws Exception {

		button1.setOnMouseClicked(this);// 单例模块的所有部件
		Label label1 = new Label("创建单例");
		label1.setTextFill(Color.web("#000000"));
		label1.setFont(new Font("Arial", 15));
		Label label11 = new Label("名称：");
		text11.setMinSize(200, 200);

		button2.setOnMouseClicked(this);// 多例模块的所有部件
		Label label2 = new Label("创建多例");
		label2.setTextFill(Color.web("#000000"));
		label2.setFont(new Font("Arial", 15));
		Label label22 = new Label("名称：");
		text22.setMinSize(200, 200);

		FlowPane flowPane1 = new FlowPane();
		flowPane1.getChildren().add(label11);
		flowPane1.getChildren().add(text1);
		flowPane1.getChildren().add(button1);
		FlowPane flowPane11 = new FlowPane();
		flowPane11.getChildren().add(text11);

		FlowPane flowPane2 = new FlowPane();
		flowPane2.getChildren().add(label22);
		flowPane2.getChildren().add(text2);
		flowPane2.getChildren().add(button2);
		FlowPane flowPane22 = new FlowPane();
		flowPane22.getChildren().add(text22);

		FlowPane flowpane = new FlowPane();
		flowpane.getChildren().add(label1);
		flowpane.getChildren().add(flowPane1);
		flowpane.getChildren().add(flowPane11);
		flowpane.getChildren().add(label2);
		flowpane.getChildren().add(flowPane2);
		flowpane.getChildren().add(flowPane22);

		Scene scene = new Scene(flowpane);

		primaryStage.setScene(scene);
		primaryStage.show();
		primaryStage.setTitle("实验二");
	}

	@Override
	public void handle(MouseEvent event) {
		// TODO Auto-generated method stub
		String name;
		String string;
		
		if (event.getSource() == button1) {
			if(count1==0) {
			name=text1.getText();
			 chairman=Chairman.getInstance(name);
			 string=name+chairman.toString()+"\n";
			 text11.appendText(string);
			 count1=1;
			}else {
				string="你已经创建了这个类的单例，不能创建新的对象了\n";
				 text11.appendText(string);
			}
		} 
		else if (event.getSource() == button2) {
			if(count2<10) {
			name=text2.getText();
			 marshal=Marshal.getInstance(name);
			 string=name+marshal.toString()+"\n";
			 text22.appendText(string);
			 count2++;
			}else {
				string="你已经创建了10个对象了，已经不能再创建了\n";
				 text22.appendText(string);
			}
		}
	}

}
